local qijie = fk.CreateSkill{
  name = "hanqing__qijie",
}

Fk:loadTranslationTable{
  ["hanqing__qijie"] = "奇截",
  [":hanqing__qijie"] = "每阶段限一次，你可以视为使用或打出【杀】或【闪】，然后交给一名其他角色一张牌并明置之。此阶段结束时，"..
  "若当前回合角色此阶段未造成过伤害，你可以将此牌当【杀】对其使用。",

  ["#hanqing__qijie"] = "奇截：视为使用或打出一张【杀】或【闪】，然后交给一名角色一张牌",
  ["#hanqing__qijie0-give"] = "奇截：交给一名角色一张牌",
  ["#hanqing__qijie-give"] = "奇截：交给一名角色一张牌，此阶段结束时你可以将之当【杀】对 %dest 使用",
  ["@@hanqing__qijie-inhand-phase"] = "奇截",
  ["#hanqing__qijie-invoke"] = "奇截：你可以将“奇截”牌当【杀】对 %dest 使用",
}

local DIY = require "packages/diy_utility/diy_utility"

qijie:addEffect("viewas", {
  pattern = "slash,jink",
  prompt = "#hanqing__qijie",
  interaction = function(self, player)
    local all_names = {"slash", "jink"}
    local names = player:getViewAsCardNames(qijie.name, all_names)
    if #names > 0 then
      return UI.CardNameBox {choices = names, all_choices = all_names}
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = qijie.name
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    if not player:isNude() and #room:getOtherPlayers(player) > 0 then
      local current = nil
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event then
        current = turn_event.data.who
      end
      local prompt = "#hanqing__qijie0-give"
      if current and current ~= player and not current.dead then
        prompt = "#hanqing__qijie-give::"..current.id
      end
      local to, card = room:askToChooseCardsAndPlayers(player, {
        min_card_num = 1,
        max_card_num = 1,
        min_num = 1,
        max_num = 1,
        targets = room:getOtherPlayers(player, false),
        skill_name = qijie.name,
        prompt = prompt,
        cancelable = false,
      })
      to = to[1]
      room:addTableMark(player, "hanqing__qijie-phase", card[1])
      DIY.showCards(to, card)
      room:moveCardTo(card, Card.PlayerHand, to, fk.ReasonGive, qijie.name, nil, true, player, "@@hanqing__qijie-inhand-phase")
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(qijie.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(qijie.name, Player.HistoryPhase) == 0
  end,
})

qijie:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(qijie.name, Player.HistoryPhase) > 0 then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event == nil then return end
      target = turn_event.data.who
      if target == player or target.dead then return end
      if #room.logic:getActualDamageEvents(1, function (e)
        local damage = e.data
        return damage.from == target
      end, Player.HistoryPhase) > 0 then return end
      return table.find(player:getTableMark("hanqing__qijie-phase"), function (id)
        return Fk:getCardById(id):getMark("@@hanqing__qijie-inhand-phase") > 0
      end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event == nil then return end
    target = turn_event.data.who
    local cards = table.filter(player:getTableMark("hanqing__qijie-phase"), function (id)
      return Fk:getCardById(id):getMark("@@hanqing__qijie-inhand-phase") > 0
    end)
    local use = room:askToUseVirtualCard(player, {
      name = "slash",
      skill_name = qijie.name,
      prompt = "#hanqing__qijie-invoke::"..target.id,
      cancelable = true,
      extra_data = {
        bypass_distances = true,
        bypass_times = true,
        extraUse = true,
        exclusive_targets = { target.id },
      },
      card_filter = {
        n = 1,
        cards = cards,
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).extra_data)
  end,
})

qijie:addAI(nil, "vs_skill")

return qijie
